package guoanmaker.operator.business.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;

import guoanmaker.operator.business.model.ProviderBusiness;
import guoanmaker.operator.business.model.ReturnResult;
import guoanmaker.operator.business.service.OperatorBusinessService;
import guoanmaker.operator.business.service.OperatorUserService;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import guoanmaker.operator.helper.common.util.OSSUnitHelper;
/**
 * 商户信息
 * 
 * @author zj
 *
 */
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping(ApiController.OPERATOR_USER)
public class OperatorBusinessInfoController {
	
	

	
	@Resource
	private  OperatorUserService operatorUserService;
	
	@Resource
	private OperatorBusinessService  operatorBusinessService;
	
	

/**
 * 	
 * @param page   分页页数 ,从0开始
 * @param size   每页的容量
 * @return
 * @throws ParseException 
 */
	/*@ApiOperation(value = "商户信息列表")
	@RequestMapping(value = "selectBusinessInfos",method = RequestMethod.POST)
	public AjaxJson selectBusinessInfos(Integer page,
            Integer size){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorBusinessService.selectBusinessInfos(page,size));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}*/
	
	@ApiOperation(value = "导出商户信息EXCEL",notes="全部导出商户")
	@RequestMapping(value ="exportAllBusinessInfos",method = RequestMethod.GET)
	public  void exportBusinessInfos(HttpServletResponse response,String  starttime,String endtime)  {
		try (OutputStream out = response.getOutputStream(); Workbook wb = operatorBusinessService.exportBusinessInfos(starttime,endtime);) {
			response.setContentType("application/octet-stream;charset=utf-8");  
			String  fileName ="商户信息";
			response.setHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes(),"iso-8859-1") + ".xls"); 
			wb.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@ApiOperation(value = "所有商户列表",notes="所有商户")
	@RequestMapping(value ="selectAllBusiness",method = RequestMethod.POST)
	public AjaxJson selectAllBusiness(String starttime,String endtime){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorBusinessService.selectByAllAccountforBusiness(starttime,endtime));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	
	
	/**
	 * 
	 * @param page                        默认为0
	 * @param size                        每页10条
	 * @param acount                      商户账号
	 * @param status                      状态
	 * @param volume                      成交量
	 * @param volumesymbol                成交量符号
	 * @param clinchadealAmount           成交金额
	 * @param clinchadealAmountsymbol     成交金额符号
	 * @param closing                     成交率
	 * @param closingsymbol               成交率符号
	 * @param serverclass                 服务种类
	 * @param shopname                    店铺名称
	 * @param servive                     业务种类
	 * @return
	 */
	@ApiOperation(value = "动态查询商户列表",notes="按照商户账号，状态，成交量，成交金额，成交率，服务种类，查询商户列表")
	@RequestMapping(value ="selectByAccountforBusiness",method = RequestMethod.POST)
	public AjaxJson selectByAccountforBusiness( Integer page,
            Integer size, String acount,String status,Integer volume, String volumesymbol, 
            Double clinchadealAmount,String clinchadealAmountsymbol,Double closing, String closingsymbol, String serverclass,String shopname,String servive){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorBusinessService.selectByAccountforBusiness(page,size
					,acount,status,volume, volumesymbol, clinchadealAmount,clinchadealAmountsymbol,closing, closingsymbol, serverclass,shopname,servive));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	
	@ApiOperation(value = "查看商户信息",notes="查看商户信息（包括账号基本信息，企业基本信息）")
	@RequestMapping(value ="viewmerchantinfo",method = RequestMethod.POST)
	public AjaxJson viewmerchantinfo(String id){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorBusinessService.viewmerchantinfo(id));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	
	@ApiOperation(value = "查看商户交易信息",notes="查看商户交易信息,  雇主身份的购买记录")
	@RequestMapping(value ="tradingInfo",method = RequestMethod.POST)
	public AjaxJson tradingInfo(String id,Integer status){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorBusinessService.tradingInfo(id,status));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	@ApiOperation(value = "查询一级和二级服务分类")
	@RequestMapping(value ="selectoneOrTwoServer",method = RequestMethod.POST)
	public AjaxJson selectoneOrTwoServer(){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorBusinessService.selectoneOrTwoServer());
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	/**
	 * 
	 * @param id
	 * @param companyName
	 * @param companyLicenseReplica
	 * @param industry
	 * @param companyRegistTime
	 * @param registereCapital
	 * @param companyfrontDeskphone
	 * @param companyEmail
	 * @param companyEnterpriseScale
	 * @param companyWeChatpublic
	 * @param companyIntroduce
	 * @return
	 */
	@ApiOperation(value = "编辑商户信息",notes="编辑商户公司基本信息")
	@RequestMapping(value="editBusinesscomInfo",method = RequestMethod.POST)
	public AjaxJson editBusinesscomInfo(String id,String companyName,String companyLicenseReplica,String industry,String companyRegistTime,
			String  registereCapital,String companyfrontDeskphone,String companyEmail,String companyEnterpriseScale,String companyWeChatpublic
			,String companyIntroduce,String companyaddress,String busname){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ProviderBusiness  providerBusiness=new  ProviderBusiness();
    		providerBusiness.setCompanyLicenseReplica(companyLicenseReplica);
			providerBusiness.setCompanyName(companyName);
			providerBusiness.setCompanyindustry(industry);
			if(companyRegistTime!=null&&!companyRegistTime.equals("")){
				String datee = companyRegistTime; 
				datee = datee.replace("Z", " UTC");//注意是空格+UTC
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
				Date date1=null;
				try {
					date1 = format.parse(datee);
				} catch (ParseException e) {
					e.printStackTrace();
				}
				providerBusiness.setCompanyRegistTime(date1);
			}
			providerBusiness.setCompanyRegisteredCapital(registereCapital);
			providerBusiness.setCompanyfrontDeskphone(companyfrontDeskphone);
			providerBusiness.setCompanyEmail(companyEmail);
			providerBusiness.setCompanyEnterpriseScale(companyEnterpriseScale);
			providerBusiness.setCompanyWeChatpublic(companyWeChatpublic);
			providerBusiness.setCompanyAdress(companyaddress);
			providerBusiness.setCompanyIntroduce(companyIntroduce);
			providerBusiness.setBusinessName(busname);
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorBusinessService.editBusinesscomInfo(id,providerBusiness));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}

	 
	 
	 @ApiOperation(value="商户上传图片返回地址信息", notes="商户上传logo返回地址信息")
	    @PostMapping(value = "/putImg")
	public  AjaxJson putImg(MultipartFile companyLogo,HttpServletResponse response) {
		  AjaxJson ajaxJson=new AjaxJson();
		 if(companyLogo!=null){
			 OSSUnitHelper ossunit=new OSSUnitHelper();
				OSSClient client=OSSUnitHelper.getOSSClient();
				String diskName = "datas/busimage/";
				String fileName = new Date().getTime() + (int) ((Math.random() * 9 + 1) * 100000)
						+ companyLogo.getOriginalFilename().substring(companyLogo.getOriginalFilename().indexOf("."));
		        String md5key = OSSUnitHelper.uploadObject2OSS(client, companyLogo, diskName,fileName);
		        String url="";
		        url=OSSUnitHelper.getUrl(client, diskName, fileName);
		        String urll=    url.substring(4, url.length());
		        url="https"+urll;
		        System.out.println(url+"5");
		        if(url!=null&&!url.equals("")){
		        	ajaxJson.setSuccess(true).setStatus(200).setData(url);
		        	response.setContentType(url);
		        } 
		 }
		 return ajaxJson;
		 
	 }
	
	
	
	@ApiOperation(value = "冻结服务商")
	@RequestMapping(value ="freezebusiness",method = RequestMethod.POST)
	public  AjaxJson freezebusiness(String id){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorBusinessService.freezebusiness(id));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	@ApiOperation(value = "解冻服务商")
	@RequestMapping(value ="jihuofreezebusiness",method = RequestMethod.POST)
	public  AjaxJson jihuofreezebusiness(String id){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorBusinessService.jihuofreezebusiness(id));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	/**
	 * 
	    * @Title: changeBusinessPassword  
	    * @Description: 输入商户id 更改密码 
	    * @param @param id
	    * @return AjaxJson      
	    * @throws
	 */
	@ApiOperation(value = "修改商户密码", notes = "修改商户密码")
	@RequestMapping(value ="changeBusinessPassword",method = RequestMethod.POST)
	public AjaxJson changeBusinessPassword(String id) {
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult = new ReturnResult();
		try {
			returnResult = operatorBusinessService.changeBusinessPassword(id);
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
		}
		return ajaxJson;
	}

}
